//
//  ResetPasswordViewController.m
//  haolaDoc
//
//  Created by WayneHao on 2017/5/16.
//  Copyright © 2017年 deyi. All rights reserved.
//

#import "ResetPasswordViewController.h"

@interface ResetPasswordViewController ()

@end

@implementation ResetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [_reFreshPasswordField addTarget:self  action:@selector(inputStatusListener)  forControlEvents:UIControlEventAllEditingEvents];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    self.resetPasswordView.layer.cornerRadius=5;
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    self.submitBtn.userInteractionEnabled=NO;
    self.submitBtn.alpha=0.4;
    self.submitBtn.backgroundColor=[AppMonitor getThemeColor];
}
//网络请求
-(void)showResult:(NSString *)status{
    
    //初始化提示框；
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:status preferredStyle:  UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //点击按钮的响应事件；
        if([status isEqual:@"密码修改成功"]){
            [[self navigationController] popViewControllerAnimated:YES];
        }
    }]];
    
    //弹出提示框；
    [self presentViewController:alert animated:true completion:nil];
    
    
}
//通过监听实现控制按钮的状态
-(void)inputStatusListener{
    if (self.oldPasswordField.text>0) {
        if(self.freshPasswordField.text==self.reFreshPasswordField.text){
            self.submitBtn.userInteractionEnabled=YES;
            self.submitBtn.alpha=1.0;
            UIColor *myColor = [UIColor colorWithRed:0 green:0.57 blue:0.89 alpha:0.800];
            [self.submitBtn setBackgroundColor:myColor];
            [self.submitBtn setTitle:@"提交" forState:UIControlStateNormal];
        }
        else{
            self.submitBtn.alpha=0.4;
            [self.submitBtn setTitle:@"新密码两次输入不一致" forState:UIControlStateNormal];
            [self.submitBtn setBackgroundColor:[UIColor redColor]];
        }
    }
    else{
        self.submitBtn.userInteractionEnabled=NO;
        [self.submitBtn setTitle:@"新密码两次输入不一致" forState:UIControlStateNormal];
        [self.submitBtn setBackgroundColor:[UIColor redColor]];
        self.submitBtn.alpha=0.4;
    }
}
-(void)dismiss{
    [SVProgressHUD dismiss];
}
- (IBAction)onSubmitBtn:(id)sender {
    NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
    NSString *userId=[ud objectForKey:@"userId"];
    if ([AppMonitor  isConnectionAvailable]) {
        _oldPassword = self.oldPasswordField.text;
        _password = self.freshPasswordField.text;
        _reNewPassword = self.reFreshPasswordField.text;
        NSString *urlStr=[NSString stringWithFormat:@"%@/doctorinfo/changePwd/%@-%@-%@",BASE_URL,_telephone,_oldPassword,_password];
        NSURL *url=[NSURL URLWithString:urlStr];
        NSLog(@"%@",url);
        NSURLRequest *request=[NSURLRequest requestWithURL:url];
        NSError *error;
        NSData *receiveData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
        NSDictionary *content = [NSJSONSerialization JSONObjectWithData:receiveData options:NSJSONReadingMutableContainers error:nil];//转换数据格式
        NSString *status= [content objectForKey:@"status"];
        if ([status isEqual:@"success"]) {
            [SVProgressHUD showWithStatus:@"密码修改成功"];
            [self performSelector:@selector(dismiss) withObject:nil afterDelay:0.7];
            [[self navigationController] popViewControllerAnimated:YES];
            //[self showResult:@"密码修改成功"];
            //   [[self navigationController] popViewControllerAnimated:YES];
        }else{
            [SVProgressHUD showWithStatus:@"修改失败，请检查"];
            [self performSelector:@selector(dismiss) withObject:nil afterDelay:0.7];
            // [self showResult:@"修改失败，请检查"];
        }
    }else{
        [SVProgressHUD showWithStatus:@"无网络连接"];
        [self performSelector:@selector(dismiss) withObject:nil afterDelay:0.7];
    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
